/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cloudptt.wp.receiver;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.view.KeyEvent;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.ui.main.MainActivity;


/**
 * Receives broadcasted intents. In particular, we are interested in the
 * android.media.AUDIO_BECOMING_NOISY and android.intent.action.MEDIA_BUTTON
 * intents, which is broadcast, for example, when the user disconnects the
 * headphones. This class works because we are declaring it in a
 * &lt;receiver&gt; tag in AndroidManifest.xml.
 */
@SuppressLint("InlinedApi")
public class PTTIntentReceiver extends BroadcastReceiver {
	private static final String LOG_TAG = "MusicIntentReceiver";
	private Context mContext;
	public  static final  int KICK_EVENT = 79;

	PowerManager.WakeLock mWakelock = null;
	KeyguardLock keyguardLock = null;
	//	private KeyService mKeyService;

	@Override
	public void onReceive(Context context, Intent intent) 
	{
		mContext = context;
		String action = intent.getAction();
		Log.d(LOG_TAG, "onReceive action = " + action);
		if(action == null)
		{
			return;
		}
		if (action.equals(Intent.ACTION_MEDIA_BUTTON)) 
		{
			KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(
					Intent.EXTRA_KEY_EVENT);

			// 获得按键字节码  
			int keyCode = keyEvent.getKeyCode();  
			// 按下 / 松开 按钮  
			int keyAction = keyEvent.getAction();  
			// 获得事件的时间  
			long downtime = keyEvent.getEventTime();  

			Log.e(LOG_TAG, "11 keyaction "  + keyAction +"  keycode" +keyEvent.getKeyCode() +" downtime :"+downtime);
			switch (keyEvent.getKeyCode()) {

			case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
				// context.startService(new
				// Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
				break;
			case KeyEvent.KEYCODE_MEDIA_PLAY:
				// context.startService(new Intent(MusicService.ACTION_PLAY));
				break;
			case KeyEvent.KEYCODE_MEDIA_PAUSE:
				// context.startService(new Intent(MusicService.ACTION_PAUSE));
				break;
			case KeyEvent.KEYCODE_MEDIA_STOP:
				// context.startService(new Intent(MusicService.ACTION_STOP));
				break;
			case KeyEvent.KEYCODE_MEDIA_NEXT:
				// context.startService(new Intent(MusicService.ACTION_SKIP));
				break;
			case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
				// TODO: ensure that doing this in rapid succession actually
				// plays the
				// previous song
				// context.startService(new Intent(MusicService.ACTION_REWIND));
				break;
			case KeyEvent.KEYCODE_HEADSETHOOK:

				if(keyAction==1)
				{
					lockScreen();
				}

				Log.i(LOG_TAG, "we want to KEYCODE_HEADSETHOOK "+ keyEvent.getKeyCode());
				break;
			default:

				Log.i(LOG_TAG, "we want to know "+ keyEvent.getKeyCode());
				break;

			}

		} else if (action.equals("android.intent.action.BOOT_COMPLETED")) {
			Log.d(LOG_TAG, "android.intent.action.BOOT_COMPLETED");
			Intent ootStartIntent=new Intent(context, MainActivity.class);
			ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(ootStartIntent); 
		}            
	}

	private void lockScreen() {	   

		KeyguardManager keyguardManager = (KeyguardManager)mContext.getSystemService(mContext.KEYGUARD_SERVICE);
		PowerManager pm = (PowerManager)mContext.getSystemService(mContext.POWER_SERVICE);
		if(null == mWakelock)
		{
			mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
		}

		if(null == keyguardLock)
		{
			keyguardLock  = keyguardManager.newKeyguardLock("");
		}


		if(!pm.isScreenOn())
		{
			mWakelock.acquire();
			keyguardLock.disableKeyguard();

			Log.e(LOG_TAG, " 解锁");
		}
	}


}
